<template>
    <div class="formBox" style="width: 100%;height:100%;">
        <RxLoading v-if="Loading"></RxLoading>
        <component v-else-if="currentView" :is="currentView" v-bind="props" :key="viewKey"></component>
    </div>
</template>

<script>

import BpmInstStart from "./BpmInstStart"
import BpmTaskStart from "./BpmTaskStart"
import BpmInstDetail from "./BpmInstDetail"
import BpmInstApi from "../../../api/rxBpm/bpmInst";
import util from '@/js/Util.js'
import RxLoading from '@/components/RxLoading'
import {onMounted, onUnmounted, markRaw} from 'vue';

export default {
    name: "OpenDoc",
    mixins: [util],
    components: {
        RxLoading
    },
    props:{
        customParams:{
            type:Object,
        },
    },
    data() {
        return {
            props: {},
            currentView: '',
            actionMap: {
                start: markRaw(BpmInstStart),
                startDraft: markRaw(BpmInstStart),
                task: markRaw(BpmTaskStart),
                detail: markRaw(BpmInstDetail),
            },
            viewKey: "",
            Loading:true,
            appId:'',
            params:''
        }
    },
    created(){
        //这个是页面定制里面传参数过来的
        if(this.customParams){
           this.init(this.customParams)
        }
    },
    onLoad(option) {
        //这里是普通的打开流程
        if(this.customParams){
            return
        }
        this.params = option.params;
        this.appId = option.appId;
        if (this.params) {
            let paramsObj = this.params.routerName ? this.params : JSON.parse(this.params)
            console.info('--appid',this.appId,paramsObj)
            this.init(paramsObj);
        }
        onMounted(() => {
            this.$eventBus.$on('flowEvent', this.handEvent);
        });
        onUnmounted(() => {
            this.$eventBus.$off('flowEvent', this.handEvent);
        });
    },
    methods: {
        init(params) {
            let startInst = false;
            let routerName = params.routerName;
            let type = routerName;
            if (routerName == "newDoc" || routerName == "newDocTab") {
                startInst = true;
                type = "newDoc";
            }
            if (routerName == "openDocTab" || routerName == "openDoc") {
                type = "openDoc";
            }
            let action = params.action;
            let from = params.from;
            let taskId = params.taskId || "";
            let id = startInst ? params.defKey : params.instId;
            let preview = JSON.parse(params.preview || false);
            let startUserId = params.startUserId;
            BpmInstApi.getInfoByDefKeyInstId(type, id, action, taskId).then(res => {
                if (!res.success) {
                    return;
                }
                this.Loading = false
                this.viewKey = util.randomId();
                this.currentView = this.actionMap[res.action];
                if (this.appId) {
                    this.props['appId'] = this.appId
                }
                console.log(res.action)
                if (res.action == "start") {
                    let conf = {defKey: id};
                    this.props.config = conf;
                    this.props.preview = preview;
                    this.props.startUserId = startUserId;
                }
                if (res.action == "startDraft") {
                    this.props.instId = res.instId;
                    this.props.config = {defKey: res.defKey};
                }
                if (res.action == "task") {
                    this.props.taskId = res.taskId;
                    this.props.instId = res.instId;
                }
                if (res.action == "detail") {
                    this.props.instId = res.instId;
                    if (from) {
                        this.props.from = from;
                    }
                }
            })
        },
        handEvent(event) {
            let appId = this.$route.query.appId;
            if (event.action == "start") {
                let instId = event.data.instId;
                this.pushInst(instId, appId);
            }
            if (event.action == "task") {
                let instId = event.data;
                this.pushInst(instId, appId);
            }
            if (event.action == "inst") {
                let instId = event.data;
                this.pushInst(instId, appId);
            }

        }
    }
}
</script>

<style scoped>
.formBox >>> .RxLayout-footer {
    padding: 0px !important;
}
.formBox>>>.RxLayout-center{
    padding: 0rem!important;
}
</style>
